package com.lian.hope.controller;

import com.lian.hope.annotations.RequiredPermission;
import com.lian.hope.enums.PermissionType;
import com.lian.hope.model.dto.VolunteerActivitiesDTO;
import com.lian.hope.model.vo.PageBean;
import com.lian.hope.model.vo.VolunteerActivitiesVO;
import com.lian.hope.service.VolunteerActivitiesService;
import com.lian.hope.utils.JwtUtil;
import com.lian.hope.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/activities")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "志愿者活动模块相关接口",description = "志愿者活动模块相关接口")
public class ActivitiesController {

    private final VolunteerActivitiesService volunteerActivitiesService;

    /**
     * 分页获取志愿者活动
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    @Operation(summary = "分页获取志愿者活动")
    @Parameters({
        @Parameter(description = "页码"),
        @Parameter(description = "每页数量")
    })
    public Result<PageBean<VolunteerActivitiesVO>> listActivity( Integer pageNum, Integer pageSize) {
        PageBean<VolunteerActivitiesVO> volunteerActivities = volunteerActivitiesService.listVolunteerActivities(
            pageNum, pageSize);
        return Result.success(volunteerActivities);
    }

    /**
     * 根据活动id获取项目详情
     * @param activityId
     * @return
     */
    @GetMapping("/{activityId}/detail")
    @Operation(summary = "根据活动id获取活动详情")
    public Result<VolunteerActivitiesVO> getActivityDetailById(
        @Parameter(description = "活动id")
        @PathVariable Long activityId) {
        VolunteerActivitiesVO volunteerActivities = volunteerActivitiesService.getVolunteerActivitiesDetailById(
            activityId
            );
        return Result.success(volunteerActivities);
    }

    /**
     * 新增志愿者活动
     *
     * @param volunteerActivitiesDTO
     * @return
     */
    @PostMapping
    @RequiredPermission(PermissionType.ACTIVITY)
    @Operation(summary = "新增活动")
    public Result insertActivity(@RequestBody VolunteerActivitiesDTO volunteerActivitiesDTO) {
        log.info("volunteerActivitiesDTO:{}",volunteerActivitiesDTO);
        volunteerActivitiesService.insertVolunteerActivities(volunteerActivitiesDTO);
        return Result.success();
    }

    /**
     * 随机获取五个活动
     *
     * @return
     */
    @GetMapping("/random")
    @Operation(summary = "随机获取五个活动")
    public Result<List<VolunteerActivitiesVO>> getRandomFiveActivities() {
        List<VolunteerActivitiesVO> volunteerActivitiesVO = volunteerActivitiesService.getRandomVolunteerActivities();
        return Result.success(volunteerActivitiesVO);
    }

    /**
     * 报名活动
     * @param activityId
     * @param Authorization
     * @return
     */
    @PostMapping("/{activityId}/signup")
    @Operation(summary = "报名活动")
    public Result insertActivitySignUp(
        @RequestHeader String Authorization, @Parameter(description = "活动id") @PathVariable Long activityId) {

        Map<String, Object> claims = JwtUtil.parseToken(Authorization);
        log.info("claims:{}", claims);
        Long userId = Long.parseLong(claims.get("userId").toString());
        volunteerActivitiesService.insertVolunteerRegister(activityId,userId);
        return Result.success();
    }

    /**
     * 取消报名活动
     * @param activityId
     * @param Authorization
     * @return
     */
    @DeleteMapping("/signup/cancel")
    @Operation(summary = "取消报名活动")
    public Result deleteActivitySignUp( @Parameter(description = "活动id") Long activityId,
        @RequestHeader String Authorization) {
        Map<String, Object> claims = JwtUtil.parseToken(Authorization);
        Long userId = Long.parseLong(claims.get("userId").toString());
        volunteerActivitiesService.deleteVolunteerRegister(activityId,userId);
        return Result.success();
    }


}
